# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
Contains a set of rules to identify superclass/subclass relationships

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


from mapperRule import MapperRule
from mapperQuestion import MapperQuestion
from infoProvider import SimpleInfoProvider
from log import log
log.debug("Loaded...")
     
            
class TransitivityRule(MapperRule):
    "Detects if a relation is transitive"

    def __init__(self, db):
        
        # Heuristics to identify generalization/specialization rules
        rules = [
                 "inferenceGroup(transitivityRule, "
                    "[transitiveGroup(_, _, _, _)])",
                 "inferenceGroup(transitivityRelationRule,"
                    "[transitiveRelation(_)])",
                 
                 "transitivityCandidatesGen(ToR, FromR, Label) :-"
                    "positive(relation(ToR)),"
                    "toPartRelation(ToR, Class),"
                    "fromPartRelation(FromR, Class),"
                    "ToR \== FromR,"
                    "label(ToR, Label),"
                    "label(FromR, Label),"
                    "positive(relation(FromR)),"
                    "positive(class(Class))",

                 "transitivityCandidates(R, Label) :- "
                    "transitivityCandidatesGen(R, _, Label)",
                    
                 "transitivityCandidates(R, Label) :- "
                    "transitivityCandidatesGen(_, R, Label)",
                    
                  "fromOfLabel(Label, C) :- "
                    "transitivityCandidates(R, Label),"
                    "fromPartRelation(R, C)",
                                
                  "fromOfTransitive(Label, FromSet) :-"
                    "setof(C, fromOfLabel(Label, C), FromSet)",
                  
                  "toOfLabel(Label, C) :- "
                    "transitivityCandidates(R, Label),"
                    "toPartRelation(R, C)",
                                
                  "toOfTransitive(Label, ToSet) :-"
                    "setof(C, toOfLabel(Label, C), ToSet)",
                  
                  "transitiveGroup(Set, FromSet, ToSet, Label) :-"
                    "setof(R, transitivityCandidates(R, Label), Set),"
                    "fromOfTransitive(Label, FSet),"
                    "toOfTransitive(Label, TSet),"
                    "subtract(FSet, TSet, FromSet),"
                    "subtract(TSet, FSet, ToSet)",
                
                  "isTrue(unification(Label, Set)) :- "
                    "isTrue(transitiveGroup(Set, _, _, Label))",
                    
                  "isTrue(transitiveRelation(Relation)) :- "
                    "isTrue(transitiveGroup([Relation|_], _, _, _))",
                
                  "isTrue(relation(Relation)) :- "
                    "isTrue(transitiveGroup([Relation|_], _, _, _))",
                    
                  "isTrue(class(Concept)) :- "
                    "isTrue(transitiveRelation(Relation)),"
                    "fromPartRelation(Relation, Concept)",
                    
                  "isTrue(class(Concept)) :- "
                    "isTrue(transitiveRelation(Relation)),"
                    "toPartRelation(Relation, Concept)",
                    
                  "transitivePropertyOWL(Relation) :- "
                    "isTrue(transitiveRelation(Relation))",
                    ]
         
        MapperRule.__init__(self, "TransitiveRelations", db, rules)
        
    def getQuestions(self):
        "Queries the KB to find possible transitive relations"
        
        questions = []
        query = "possible(transitiveGroup(Set, FromSet, ToSet, Label))," \
                "Set = [Relation|_]"
        transitives = self.db.query(query)
        
        for answer in transitives:
            answer["Ids"] = list(answer["Set"])
            answer["Ids"].extend(answer["FromSet"])
            answer["Ids"].extend(answer["ToSet"])
            questions.append(MapperQuestion(
                    self.db,
                    answer,
                    'Because the relation "<b>%(Relation)s</b>" goes from one '
                        'concept to another in a row, does it make sense to '
                        'say that "<b>%(FromSet)s</b>" "<b>%(Relation)s</b>" '
                        '"<b>%(ToSet)s</b>" ?',
                     'transitiveGroup(%(Set)s, %(FromSet)s, %(ToSet)s, '
                        '%(Label)s)',
                     helpFile = "transitivity",
              ))
            
        return questions
      
    def getInfoProvider(self):
        "Returns an info provider about transitive relations"
        
        return SimpleInfoProvider(self.db,
                            "Rel = %s,"
                                "((isTrue(transitiveRelation(Relation)),"
                                   "inEquals(Relation, Rel));"
                                   "isTrue(transitiveRelation(Rel)))",
                            "This relation is transitive")
        
